﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraCharts;
using DevExpress.Utils;
using TierService;
using OrderMgr.SysSetting;
using System.IO;
using System.Data.SqlClient;
using TierApplication;


namespace OrderMgr.SysSetting
{
    public partial class frmProductDetail : TierService.frmTemplate
    {
        #region "Member Variables"
        public string sFuncID;
        public TierService.Utility.OperateMode OpMode;
        public string sUserID;
        public string _ProductID = "";
        T_Product mProduct = new T_Product();
        public bool IsPatientSearch = true;


        SqlDataAdapter sdaProductDetail = new SqlDataAdapter();
        DataSet dsProductDetail = new DataSet();
        #endregion

        public frmProductDetail()
        {
            InitializeComponent();
        }

        private void frmProductDetail_Load(object sender, EventArgs e)
        {
            initForm();
           
      

            switch (OpMode)
            {
                case Utility.OperateMode.Query:
                    btnSave.Enabled = false;
                    showInfo(_ProductID);
                    break;
                case Utility.OperateMode.Insert:
                    txtProductID.Focus();
                    break;
                case Utility.OperateMode.Update:
                    txtProductID.BackColor = Color.Silver;
                    txtProductID.Properties.ReadOnly = true;
                    txtProductName.Focus();
                    btnSave.Enabled = true;
                    showInfo(_ProductID);
                    break;
                case Utility.OperateMode.Delete:
                    break;
                default:
                    break;
            }

        
        }

        private void initForm()
        {
            Utility.DataToLookupComboxEx(txtUnit, C_DMB.GetListByType(C_DMB.DType.Unit.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtSupplierID, (new T_Supplier()).GetList(""), "SupplierID", "SupplierName");
            Utility.DataToLookupComboxEx(txtProductType, C_DMB.GetListByType(C_DMB.DType.ProductType.ToString()), "CodeID", "CodeName");

            //StringBuilder strSql = new StringBuilder();
            //strSql.Append("  select");
            //strSql.Append("  ProductID 商品编码,ProductName 商品名称,");
            //strSql.Append("  (select CodeName from C_ProductType where codeid=a.ProductType) 商品类别,UnitPrice 单价,");
            //strSql.Append("  Color 颜色,Size 尺寸,Spec1 规格");
            //strSql.Append(" FROM T_Product a  ");
            //strSql.Append(" FROM T_Product a  where ProductProp='1' ");

        }


        private void showInfo(string sID)
        {
            mProduct.GetModel(sID);

            this.txtProductID.Text = mProduct.ProductID;
            this.txtProductNo.Text = mProduct.ProductNo;
            this.txtProductName.Text = mProduct.ProductName;
            //this.txtProductNameEn.Text = mProduct.ProductNameEn;
            this.txtProductType.EditValue = mProduct.ProductType;
            this.txtSupplierID.EditValue = mProduct.SupplierID;
            this.txtSpec1.Text = mProduct.Spec1;
            //this.txtSpec2.Text = mProduct.Spec2;
            //this.txtSpec3.Text = mProduct.Spec3;
            this.txtUnit.EditValue = mProduct.Unit;
            this.txtUnitPrice.Text = mProduct.UnitPrice.ToString();
            //this.txtUnitPriceOut.Text = mProduct.UnitPriceOut.ToString();
            this.txtDepotUp.Text = mProduct.DepotUp.ToString();
            this.txtDepotDown.Text = mProduct.DepotDown.ToString();
            //this.txtPhoto.Text = mProduct.Photo.ToString();
            this.txtColor.Text = mProduct.Color;
            this.txtSize.Text = mProduct.Size;
            this.txtRemark.Text = mProduct.Remark;
            txtUsefulLifeType.EditValue = mProduct.UsefulLifeType;
            txtBaseProductPlace.EditValue = mProduct.BaseProductPlace;
            //this.txtPoint.Text = mProduct.Point.ToString();
            //this.txtProductProp.Text = mProduct.ProductProp;
            //this.txtCreateDate.Text = mProduct.CreateDate.ToString();
            //this.txtCreateUser.Text = mProduct.CreateUser;
            //txtEndQuantity.Value = mProduct.EndQuantity;
            //txtAvgprice.Value = mProduct.Avgprice;

            //BindProductDetail("");

          
            //if ((string)txtProductType.EditValue!="0")
            //{
            //    gbMProduct.Visible = false;
            //    Height = Height - gbMProduct.Height;
            //}
           
        }

        private void BindProductDetail(string bookmark)
        {

            //if (_ProductID == "") return;

            SqlConnection conn = new SqlConnection(DBUtility.DbHelperSQL.connectionString);

            SqlCommand cmd;
            SqlParameter pram;
            StringBuilder strSql = new StringBuilder();

            string sSql = "";
            sSql = "select a.ProductDetailID,a.ProductID,a.MProductID,b.UnitPrice,a.Quantity,(b.UnitPrice*a.Quantity) SumPrice,b.ProductName ";
            sSql = sSql + "from T_ProductDetail a,T_Product b where a.MProductID=b.ProductID and a.ProductID='" + _ProductID + "'";
            cmd = new SqlCommand(sSql, conn);
            sdaProductDetail.SelectCommand = cmd;

            strSql.Append("insert into T_ProductDetail(");
            strSql.Append("ProductDetailID,ProductID,MProductID,Quantity)");
            strSql.Append(" values (");
            strSql.Append("@ProductDetailID,@ProductID,@MProductID,@Quantity)");

            cmd = new SqlCommand(strSql.ToString(), conn);
            pram = new SqlParameter("@ProductID", SqlDbType.VarChar, 20);
            pram.Value = _ProductID;
            cmd.Parameters.Add(pram);
            cmd.Parameters.Add("@ProductDetailID", SqlDbType.VarChar, 20, "ProductDetailID");
            cmd.Parameters.Add("@MProductID", SqlDbType.VarChar, 20, "MProductID");
            cmd.Parameters.Add("@Quantity", SqlDbType.Decimal, 9, "Quantity");
            sdaProductDetail.InsertCommand = cmd;

            strSql.Remove(0, strSql.Length);
            strSql.Append("update T_ProductDetail set ");
            //strSql.Append("ProductID=@ProductID,");
            strSql.Append("MProductID=@MProductID,");
            strSql.Append("Quantity=@Quantity");
            strSql.Append(" where ProductDetailID=@ProductDetailID ");
            cmd = new SqlCommand(strSql.ToString(), conn);
            cmd.Parameters.Add("@ProductDetailID", SqlDbType.VarChar, 20, "ProductDetailID");
            cmd.Parameters.Add("@MProductID", SqlDbType.VarChar, 20, "MProductID");
            cmd.Parameters.Add("@Quantity", SqlDbType.Decimal, 9, "Quantity");
            sdaProductDetail.UpdateCommand = cmd;

            cmd = new SqlCommand("delete T_ProductDetail where ProductDetailID=@ProductDetailID", conn);
            cmd.Parameters.Add("@ProductDetailID", SqlDbType.VarChar, 20, "ProductDetailID");
            sdaProductDetail.DeleteCommand = cmd;


         
        }

        private void GetFormReturValue(string str)
        {
            string sID = str;
            if (sID != "")
            {
                showInfo(sID);
            }
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            //Close();
            DialogResult = System.Windows.Forms.DialogResult.Cancel ;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            mProduct.ProductID = this.txtProductID.Text;
            mProduct.ProductNo = this.txtProductNo.Text;
            mProduct.ProductName = this.txtProductName.Text;
            //mProduct.ProductNameEn = this.txtProductNameEn.Text;
            mProduct.ProductType = (string)this.txtProductType.EditValue;
            mProduct.SupplierID = (string)this.txtSupplierID.EditValue;
            mProduct.Spec1 = this.txtSpec1.Text;
            //mProduct.Spec2 = this.txtSpec2.Text;
            //mProduct.Spec3 = this.txtSpec3.Text;
            mProduct.Unit = (string)this.txtUnit.EditValue;
            mProduct.UnitPrice = decimal.Parse(this.txtUnitPrice.Text);
            //mProduct.UnitPriceOut = decimal.Parse(this.txtUnitPriceOut.Text);
            mProduct.DepotUp = decimal.Parse(this.txtDepotUp.Text);
            mProduct.DepotDown = decimal.Parse(this.txtDepotDown.Text);
            mProduct.Color = this.txtColor.Text;
            mProduct.Size = this.txtSize.Text;
            mProduct.Remark = this.txtRemark.Text;
            mProduct.UsefulLifeType = txtUsefulLifeType.Text;
            mProduct.BaseProductPlace = txtBaseProductPlace.Text;
            //mProduct.Point = decimal.Parse(this.txtPoint.Text);
            //mProduct.ProductProp = this.txtProductProp.Text;
            //DateTime CreateDate = DateTime.Parse(this.txtCreateDate.Text);
            //mProduct.CreateUser = this.txtCreateUser.Text;

            //if (gProductType == Utility.ProductType.MProduct)
            //    mProduct.ProductProp = "1";
            //if (gProductType == Utility.ProductType.Product)
            //    mProduct.ProductProp = "2";


            if ((mProduct.ProductID == "") || (mProduct.ProductID == null))
            {
                txtProductID.Focus();
                XtraMessageBox.Show("商品规格型号不能为空,请输入");
                return;
            }
            if ((mProduct.ProductName == ""))
            {
                XtraMessageBox.Show("产品名称不能为空,请输入!");
                txtProductName.Focus();
                return;
            }


            switch (OpMode)
            {
                case TierService.Utility.OperateMode.Insert:
                    try
                    {
                        //mProduct.CustID = Utility.GetSeqval(Utility.SeqName.CustID.ToString());
                        mProduct.Add();
                       
                        txtProductID.BackColor = Color.Silver;
                        txtProductID.Enabled = false;
                        _ProductID = txtProductID.Text;
                        BindProductDetail("");
                        OpMode = Utility.OperateMode.Update;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("新增出错,错误原因为:" + ex.Message);
                        break;
                    }
                    MessageBox.Show("商品信息新增成功!");
                    //DialogResult = DialogResult.OK;
                    break;
                case TierService.Utility.OperateMode.Update:
                
                    try
                    {
                        mProduct.ProductID = txtProductID.Text;
                        mProduct.Update();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("商品信息修改出错,错误原因为:" + ex.Message);
                        break;
                    }
                    MessageBox.Show("商品信息修改成功!");
                    //DialogResult = DialogResult.OK;
                    break;
                default:
                    break;
            }
               //DialogResult = System.Windows.Forms.DialogResult.OK;
            //Close();
        }

       


    }
}